package cz.tomasdvorak.cars.dto;

import cz.tomasdvorak.cars.url.VehicleImageUrlCreator;
import cz.tomasdvorak.cars.utils.Sets;

import java.io.Serializable;
import java.util.Collection;
import java.util.Set;

public class Vehicle implements Serializable {
    private int id;
    private String manufacturer;
    private String registrationNumber;
    private String model;
    private Integer price;
    private Integer gasCapacity;
    private VehicleBodyType type;
    private int ownerId;
    private Set<Integer> users;
    private boolean hasImage;
    private VehicleImageUrlCreator imageUrl;

    public Vehicle(int id, String registrationNumber, String manufacturer, String model, Integer price, Integer gasCapacity,
                   VehicleBodyType type, int ownerId, Set<Integer> users, boolean hasImage, VehicleImageUrlCreator imageUrlCreator) {
        this.manufacturer = manufacturer;
        this.model = model;
        this.price = price;
        this.gasCapacity = gasCapacity;
        this.type = type;
        this.id = id;
        this.registrationNumber = registrationNumber;
        this.ownerId = ownerId;
        this.users = users == null ? Sets.<Integer>hashSet() : Sets.hashSet(users);
        this.hasImage = hasImage;
        this.imageUrl = imageUrlCreator;
    }

    public Vehicle() {
        this.id = -1;
    }

    public String getManufacturer() {
        return manufacturer;
    }

    public void setManufacturer(String manufacturer) {
        this.manufacturer = manufacturer;
    }

    public String getModel() {
        return model;
    }

    public void setModel(String model) {
        this.model = model;
    }

    public Integer getPrice() {
        return price;
    }

    public void setPrice(Integer price) {
        this.price = price;
    }

    public Integer getGasCapacity() {
        return gasCapacity;
    }

    public void setGasCapacity(Integer gasCapacity) {
        this.gasCapacity = gasCapacity;
    }

    public VehicleBodyType getType() {
        return type;
    }

    public void setType(VehicleBodyType type) {
        this.type = type;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public void setRegistrationNumber(String registrationNumber) {
        this.registrationNumber = registrationNumber;
    }

    public boolean isNew() {
        return id == -1;
    }

    public int getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(int ownerId) {
        this.ownerId = ownerId;
    }

    public void setUsers(Set<Integer> users) {
        this.users = Sets.hashSet(users);
    }

    public void setUsers(Collection<Integer> users) {
        this.users = Sets.hashSet(users);
    }

    public Set<Integer> getUsers() {
        return Sets.hashSet(this.users);
    }

    public void setImageUrlCreator(VehicleImageUrlCreator imageUrl) {
        this.imageUrl = imageUrl;
    }

    public String getImageUrl() {
        if(imageUrl != null) {
            return imageUrl.create(this);
        }
        return null;
    }

    public String getImageThumbUrl() {
        if (imageUrl != null) {
            return imageUrl.create(this, true);
        }
        return null;
    }

    public boolean hasImage() {
        return imageUrl != null && hasImage;
    }

    @Override
    public String toString() {
        return manufacturer + " " +  model + " (" + registrationNumber + ")";
    }
}
